package com.qidian.mes.md.service.impl;

import com.alibaba.fastjson2.JSON;
import com.qidian.mes.md.mapper.MaterialMapper;
import com.qidian.mes.md.mapper.ProductMapper;
import com.qidian.mes.md.service.MaterialAndProductService;
import com.qidian.mes.md.vo.*;
import com.qidian.mes.pojo.Material;
import com.qidian.mes.pojo.Product;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CoolPro
 * @Description: 产品物料服务实现类
 * @Time: 2024-04-04 11:50
 **/
@Service
public class MaterialAndProductServiceImpl implements MaterialAndProductService {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private ProductMapper productMapper;


    @Override
    public QdResult<List<MaterialAndProductVO>> queryByParam(String name, Integer classifyId, String model,String stockStatus, Integer page, Integer limit,Integer tag) {
        if (tag == 1){
            return QdResult.success(materialMapper.findAll(name, classifyId, model,Integer.valueOf(stockStatus)));
        }
        if(tag == 0){
            QdResult<List<MaterialAndProductVO>> result = new QdResult<>();
            List<MaterialAndProductVO> list = new ArrayList<>();
            List<MaterialAndProductVO> allResults = new ArrayList<>();

            // 获取所有符合条件的数据
            allResults.addAll(materialMapper.findAll(name, classifyId, model,Integer.valueOf(stockStatus)));
            allResults.addAll(productMapper.findAll(name, classifyId, model,Integer.valueOf(stockStatus)));

            // 计算分页的起始索引和结束索引
            int startIndex = (page - 1) * limit;
            int endIndex = Math.min(startIndex + limit, allResults.size());

            // 获取当前页的数据
            if (startIndex < allResults.size()) {
                list = allResults.subList(startIndex, endIndex);
            }

            result.setData(list);
            result.setCode(200);
            result.setMsg(String.valueOf(allResults.size()));
            System.out.println("当前页数据条数：" + allResults.size());
            return result;
        }
        return QdResult.error();

    }

    @Override
    public QdResult<List<ProductOrderVO>> queryByProductId(Integer productId) {
        return QdResult.success(productMapper.queryByProductId(productId));
    }

    @Override
    public QdResult<List<PlannedAndCompletionVO>> queryPlannedAndCompletionByProductId(Integer idProduct) {
        List<PlannedAndCompletionVO> plannedAndCompletionVOS = productMapper.queryPlannedAndCompletionByProductId(idProduct);

        return QdResult.success(plannedAndCompletionVOS);
    }

    // 更新产品、物料状态
    @Override
    public QdResult updateMaterialAndProductStatus(Integer id, String status, Integer category) {
        if (category == 0) {
            productMapper.updateStatus(id, status);
            return QdResult.success();
        }
        if (category == 1) {
            materialMapper.updateStatus(id, status);
            return QdResult.success();
        }
        return QdResult.error("物品类型不匹配");
    }

    @Override
    public QdResult updateMaterialAndProductInventory(Integer id, Integer inventory, Integer category) {
        if (category == 0) {
            productMapper.updateInventory(id, inventory);
            return QdResult.success();
        }
        if (category == 1) {
            materialMapper.updateInventory(id, inventory);
            return QdResult.success();
        }
        return QdResult.error("物品类型不匹配");
    }

    @Override
    public QdResult updateMaterialAndProductClassify(Integer id, Integer classifyId, Integer category) {
        if (category == 0) {
            productMapper.updateClassify(classifyId, id);
            return QdResult.success();
        }
        if (category == 1) {
            materialMapper.updateClassify(classifyId, id);
            return QdResult.success();
        }
        return QdResult.error("物品类型不匹配");

    }

    @Override
    public QdResult addMaterialAndProduct(String data, Integer category) {
        if (category == 0) {
            Product product = JSON.parseObject(data, Product.class);
            System.out.println(product+">>>>>>>>>>>>>>>>>>>>>>>>>请求信息");
            productMapper.insert(product);
            return QdResult.success();
        }
        if (category == 1) {
            Material material = JSON.parseObject(data, Material.class);
            System.out.println(material+">>>>>>>>>>>>>>>>>>>>>>>>>请求信息");
            materialMapper.insert(material);
            return QdResult.success();
        }
        return QdResult.error("添加失败");
    }

    @Override
    public QdResult updateMaterialAndProduct(String data, Integer category) {

        if (category == 0) {
            Product product = JSON.parseObject(data, Product.class);
            System.out.println(product+">>>>>>>>>>>>>>>>>>>>>>>>>请求信息");

            productMapper.update(product);
            return QdResult.success();
        }
        if (category == 1) {
            Material material = JSON.parseObject(data, Material.class);
            System.out.println(material+">>>>>>>>>>>>>>>>>>>>>>>>>请求信息");
            materialMapper.update(material);
            return QdResult.success();
        }
        return QdResult.error(400,"修改失败");
    }

    @Override
    public QdResult deleteMaterialAndProduct(Integer id, Integer category) {
        if (category == 0) {
            productMapper.updateStatus(id,"-1");

            return QdResult.success();
        }
        if (category == 1) {
            materialMapper.updateStatus(id,"-1");
            return QdResult.success();
        }
        return QdResult.error("参数异常");
    }
}
